import { Events } from '@ionic/angular';
import { DomSanitizer } from '@angular/platform-browser';
import { UserVO } from './../vo/UserVO';
import { ApiUrl } from './../api/ApiUrl';
import { AppUtils } from './../api/AppUtils';
import { Component, OnInit, AfterViewChecked } from '@angular/core';

@Component({
  selector: 'app-instantmessage',
  templateUrl: './instantmessage.page.html',
  styleUrls: ['./instantmessage.page.scss'],
})
export class InstantmessagePage implements OnInit, AfterViewChecked {
  username: string;
  password: string;
  url: string;
  csafeurl: any;
  constructor(private appUtils: AppUtils, private sanitizer: DomSanitizer, private events: Events) {
    this.csafeurl = this.sanitizer.bypassSecurityTrustResourceUrl('assets/html/noinstantmessage.html');
    let userVO: UserVO = ApiUrl.user;
    if (null != userVO && (userVO.iusertype == '1' || userVO.iusertype == '2')) {
      this.appUtils.presentLoading('即时通信加载中...');
      ApiUrl.txflag = true;
      this.username = userVO.cname;
      this.password = userVO.cname;
      this.url = ApiUrl.txbaseUrl + this.username + '/' + this.password;
      // this.url = ApiUrl.txbaseUrl + 'admin' + '/' + 'admin';
      this.csafeurl = this.sanitizer.bypassSecurityTrustResourceUrl(this.url);
      console.log(this.url);
    } else {
      console.log('用户未登录');
    }
    // 订阅 改变值的事件
    this.events.subscribe('userlogin:change', (boolean) => {
      if (boolean) {
        userVO = ApiUrl.user;
        if (null != userVO && !ApiUrl.txflag && (userVO.iusertype == '1' || userVO.iusertype == '2')) {
          this.appUtils.presentLoading('即时通信加载中...');
          ApiUrl.txflag = true;
          this.username = userVO.cname;
          this.password = userVO.cname;
          this.url = ApiUrl.txbaseUrl + this.username + '/' + this.password;
          // this.url = ApiUrl.txbaseUrl + 'admin' + '/' + 'admin';
          this.csafeurl = this.sanitizer.bypassSecurityTrustResourceUrl(this.url);
          console.log(this.url);
        } else {
          console.log('用户未登录');
        }
      } else {
        this.csafeurl = this.sanitizer.bypassSecurityTrustResourceUrl('assets/html/noinstantmessage.html');
      }
    });
  }

  ngOnInit() {
  }
  ngAfterViewChecked() {
  }


}
